package com.example.myblog.controller;

import com.example.myblog.pojo.Article;
import com.example.myblog.pojo.Comment;
import com.example.myblog.pojo.User;
import com.example.myblog.service.ArticleService;
import com.example.myblog.service.CommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: MyBlog
 * @BelongsPackage: com.example.myblog.controller
 * @ClassName ArticleController
 * @CreateTime: 2025-05-31  21:05
 * @Author: TangLiFei
 */
@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;

    // 添加
    @GetMapping("/addArticle")
    public String addForm() {
        return "add";
    }

    @PostMapping("/add")
    public String addArticle(Article article, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) {
            article.setAuthor(user.getAccount());
        }
        article.setCreateTime(new Date());
        articleService.add(article);
        return "redirect:/allArticle";
    }

    // 编辑页面
    @GetMapping("/editArticle")
    public String editForm(int id, Model model){
        Article article = articleService.findById(id);
        model.addAttribute("article", article);
        return "update";
    }

    // 编辑处理
    @PostMapping("/edit")
    public String editArticle(Article article) {
        article.setModifieTime(new Date());
        articleService.update(article);
        return "redirect:/Articlebackend";
    }

    // 删除处理
    @GetMapping("/deleteArticle")
    public String deleteArticle(int id) {
        articleService.delete(id);
        return "forward:/Articlebackend";
    }

    //搜索框
    @GetMapping("/searchArticle")
    public String searchArticle(@RequestParam("keyword") String keyword, Model model) {
        List<Article> articles = articleService.search(keyword);
        model.addAttribute("articles", articles);
        return "allarticle";
    }

    @RequestMapping("/allArticle")
    public String showblog(Model model){
        List<Article> articles = articleService.findAll();
        model.addAttribute("articles", articles);
        System.out.println("访问所有博客页面");
        return "allarticle";
    }

   // 显示文章详情并附带评论
   @GetMapping("/articleDetail")
   public String showArticleDetail(@RequestParam("id") int id, Model model) {
       // 根据文章 ID 查询文章信息
       Article article = articleService.findById(id);
       if (article != null) {
           // 将文章信息添加到模型中
           model.addAttribute("article", article);
           // 查询该文章的评论
           PageInfo<Comment> commentPageInfo = commentService.getComments(id, 1, 10);
           List<Comment> comments = commentPageInfo.getList();
           model.addAttribute("comments", comments);
       }
       return "articleDetail";
   }
}